﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoneImp {
	public class Slot {
		public SlotData data;
		SkeletonBase skeleton;
		public Bone bone;
		public float r, g, b, a;
		public Attachment attachment;

		//Was part of internal class
		private float attachmentTime;

		public Slot(SlotData data, SkeletonBase skeleton, Bone bone) {
			this.data = data;
			this.skeleton = skeleton;
			this.bone = bone;
			setToBindPose();
		}

		/* @param attachment May be 0 to clear the attachment for the slot. */
		public void setAttachment(Attachment attachment) {
			this.attachment = attachment;
			this.attachmentTime = this.skeleton.time;
		}

		void setAttachmentTime(float time) {
			this.attachmentTime = this.skeleton.time - time;
		}

		float getAttachmentTime() {
			return this.skeleton.time - this.attachmentTime;
		}

		public void setToBindPose() {
			this.r = this.data.r;
			this.g = this.data.g;
			this.b = this.data.b;
			this.a = this.data.a;

			Attachment attachment = null;
			if (this.data.attachmentName != null) {
				// Find slot index.
				int i;
				for (i = 0; i < this.skeleton.data.slots.Count; ++i) {
					if (this.data == this.skeleton.data.slots[i]) {
						attachment = this.skeleton.getAttachmentForSlotIndex(i, this.data.attachmentName);
						break;
					}
				}
			}
			setAttachment(attachment);
		}
	}
}
